<html>
<head><meta charset="utf-8"><title>Inline assembly in MIR · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Inline.20assembly.20in.20MIR.html">Inline assembly in MIR</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="188320099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Inline%20assembly%20in%20MIR/near/188320099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Inline.20assembly.20in.20MIR.html#188320099">(Feb 16 2020 at 09:56)</a>:</h4>
<p>Could I get some opinions from a MIR expert for <a href="https://github.com/rust-lang/rust/issues/69171" target="_blank" title="https://github.com/rust-lang/rust/issues/69171">#69171</a>? Specifically this question:</p>
<blockquote>
<p>I'm not sure how best to represent inline assembly in MIR. I can think of several options:<br>
- Represent it as a <code>Statement</code>, like the previous implementation. This effectively acts as a parallel <code>Assign</code> since it reads/writes to multiple places at once for outputs.<br>
- Represent it as an <code>RValue</code> which takes its inputs as arguments and returns a tuple of outputs. The outputs are then moved to their destinations with <code>Assign</code>. This is closer to the LLVM representation, but it may be difficult to model the borrow checking behavior (we want to avoid multiple outputs to the same place).<br>
- Represent it as a <code>Terminator</code> like a <code>Call</code>. This works well because the new <code>asm!</code> has a <code>noreturn</code> flag which indicates that it never returns.t-compiler/help</p>
</blockquote>



<a name="188321591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Inline%20assembly%20in%20MIR/near/188321591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Inline.20assembly.20in.20MIR.html#188321591">(Feb 16 2020 at 10:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/131828-t-compiler/topic/Inline.20assembly.20in.20MIR/near/188320099" title="#narrow/stream/131828-t-compiler/topic/Inline.20assembly.20in.20MIR/near/188320099">said</a>:</p>
<blockquote>
<blockquote>
<ul>
<li>Represent it as a <code>Terminator</code> like a <code>Call</code>. This works well because the new <code>asm!</code> has a <code>noreturn</code> flag which indicates that it never returns.t-compiler/help</li>
</ul>
</blockquote>
</blockquote>
<p>I don't really know why <code>asm</code> isn't already a <code>Terminator</code>. If we're going to have two separate types for everything, then it may as well be done correctly.</p>



<a name="188329040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Inline%20assembly%20in%20MIR/near/188329040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Inline.20assembly.20in.20MIR.html#188329040">(Feb 16 2020 at 15:31)</a>:</h4>
<p><span class="user-mention" data-user-id="116118">@Matthew Jasper</span> Indeed, I'm trying to do things correctly this time! Can you give me your opinion on these two ways of representing InlineAsm in MIR?</p>



<a name="188329045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Inline%20assembly%20in%20MIR/near/188329045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Inline.20assembly.20in.20MIR.html#188329045">(Feb 16 2020 at 15:31)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="c1">// Input</span>
<span class="n">asm</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;&quot;</span><span class="p">,</span><span class="w"> </span><span class="k">in</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">out</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">inout</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span><span class="w"> </span><span class="n">c</span><span class="p">);</span><span class="w"></span>

<span class="c1">// Option 1</span>
<span class="n">InlineAsm</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">operands</span>: <span class="p">[</span><span class="k">in</span><span class="p">(</span><span class="n">a</span><span class="w"> </span>: <span class="nc">rvalue</span><span class="p">),</span><span class="w"> </span><span class="n">out</span><span class="p">(</span><span class="n">b</span><span class="w"> </span>: <span class="nc">place</span><span class="p">),</span><span class="w"> </span><span class="n">inout</span><span class="p">(</span><span class="n">c</span><span class="w"> </span>: <span class="nc">place</span><span class="p">)]</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="c1">// Option 2</span>
<span class="n">_0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">InlineAsm</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">inputs</span>: <span class="p">[</span><span class="n">a</span>: <span class="nc">rvalue</span><span class="p">,</span><span class="w"> </span><span class="n">c</span>: <span class="nc">rvalue</span><span class="p">]</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_0</span><span class="p">.</span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_0</span><span class="p">.</span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
</pre></div>



<a name="188329095"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Inline%20assembly%20in%20MIR/near/188329095" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Inline.20assembly.20in.20MIR.html#188329095">(Feb 16 2020 at 15:33)</a>:</h4>
<p>Both are functionally equivalent, since <code>asm!</code> only supports <code>Copy</code> arguments. My main concern is how it will integrate with the borrow checker, since we want to ban things like <code>asm!("",  out(reg) a, out(reg) a)</code>, or even <code>asm!("", out(reg) *a, out(reg) a)</code>.</p>



<a name="188331763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Inline%20assembly%20in%20MIR/near/188331763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Inline.20assembly.20in.20MIR.html#188331763">(Feb 16 2020 at 17:04)</a>:</h4>
<p>I would probably try <code>Option 1</code> first.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>